home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11953 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  102 lines

  1. Path: news.campus.mci.net!usenet
  2. From: Kevin Wilson <kevinw@telis.org>
  3. Newsgroups: comp.lang.c++
  4. Subject: Derived class question
  5. Date: Sat, 16 Mar 1996 23:27:06 -0800
  6. Organization: MCI State Government and University Systems
  7. Message-ID: <314BBECA.905@telis.org>
  8. NNTP-Posting-Host: s22-pm04.usc.campus.mci.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (Win95; I)
  13.  
  14. Hi all,
  15. I could use some help on a program I am writing for homework in a data structures class. 
  16.  I am using this class as an excuse to learn c++.
  17.  
  18. The first class below is for link-list handling and it works fine.  My next task was to 
  19. build a routine to implement stacks and one for queues.  I was planning on using the 
  20. llist class as a base for a stack class (see below).  I get a compile error about type 
  21. conversion...Pop needs to return a Stack *.    I have tried several ideas, including 
  22. typing *ptr as a Stack instead of a Llist.  But that moves the error to the Retreive 
  23. call.  I can work around it by making the char *str public in Llist, but that defeats 
  24. the purpose of protecting the strucure of Llist right?
  25.  
  26. Whats wrong with this picture??  I havent found any similar examples in my books.
  27.  
  28. Would really appreciate some help, via email preferred
  29. Kevin
  30.  
  31. kevinw@telis.org
  32.  
  33. BTW, would also appreciate any comments on my methods... anything done here thats REALLY 
  34. Stupid???
  35.  
  36.  
  37. --------------------------llist.h--------------------------
  38. #define NULL    0
  39.  
  40. class Llist{
  41.     private:
  42.         char    *str;        //points to list element
  43.         Llist    *nxt;        //points to next element in list
  44.         static Llist head;    //list head
  45.         static int nvals;   //holds number of elements in list
  46.     public:
  47.                 Llist();
  48.                 Llist(char *x, Llist *ptr);
  49.                 ~Llist();
  50.         int        Insert(char *x, int p);
  51.         int        Locate(char *x);
  52.         Llist    *Retreive(int p);
  53.         int        Delete(int p);
  54.         int        First();
  55.         int        Next(int p);
  56.         int        Previous(int p);
  57.         int        Last();
  58.         int        End();
  59.         int        MakeNull();
  60.         void    PrintList();
  61.         friend    ostream& operator<<(ostream& os, Llist& L); 
  62. };
  63.  
  64. --------------------------stack.h--------------------------
  65. //
  66. //    Stack lib using link list class as base
  67. //
  68.  
  69. #include "llist.h"
  70.  
  71. class Stack:public Llist {
  72.     public:
  73.         Stack(void);
  74.         ~Stack();
  75.         char    *Pop(void);
  76.         int        Push(char *);
  77. };
  78.  
  79. --------------------------stack.cpp--------------------------
  80. #include <iostream.h>
  81. #include "stack.h"
  82.  
  83. Stack::Stack():Llist()
  84. {}
  85. Stack::~Stack(){}
  86.  
  87. Stack *Stack::Pop()
  88. {
  89.     Llist *ptr;
  90.     
  91.     ptr = Retreive(1);
  92.     if(ptr != NULL)
  93.         Llist::Delete(1);
  94.         }
  95.     return ptr;                <<<<<<<<<<<<<<<<<<<ERROR
  96. }
  97.  
  98. int Stack::Push(char *x)
  99. {
  100.     return Llist::Insert(x, 1);
  101. }
  102.